package com.cloud.freeflow.api.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.cloud.freeflow.api.bean.WfActPart;
import com.cloud.freeflow.api.entity.ProcActPart;
import com.cloud.freeflow.api.entity.ProcPart;
import com.cloud.freeflow.common.utils.BaseMapperHelper;

public interface ProcActPartMapper extends BaseMapperHelper<ProcActPart> {

	@Select("SELECT a.part_id, a.part_name, a.part_type, if(b.part_id is null, 0, b.part_id) as relation_id "
			+ "FROM proc_part a LEFT JOIN proc_act_part b ON a.part_id = b.part_id and b.act_def_id = #{actDefId} WHERE a.proc_def_id = #{procDefId} "
			+ "ORDER BY relation_id DESC, part_id ASC")
	@Results({ @Result(property = "partId", column = "part_id", javaType = Long.class),
			@Result(property = "partName", column = "part_name", javaType = String.class),
			@Result(property = "partType", column = "part_type", javaType = String.class),
			@Result(property = "relationId", column = "relation_id", javaType = Long.class) })
	List<ProcPart> listActParts(@Param("procDefId") Long procDefId, @Param("actDefId") Long actDefId);

	@Select("SELECT a.part_id, a.part_name, a.part_type, if(b.part_id is null, 0, b.part_id) as relation_id "
			+ "FROM proc_part a LEFT JOIN proc_act_part b ON a.part_id = b.part_id WHERE a.proc_def_id = #{procDefId} and b.act_def_id = #{actDefId} "
			+ "ORDER BY relation_id DESC, part_id ASC")
	@Results({ @Result(property = "partId", column = "part_id", javaType = Long.class),
			@Result(property = "partName", column = "part_name", javaType = String.class),
			@Result(property = "partType", column = "part_type", javaType = String.class),
			@Result(property = "relationId", column = "relation_id", javaType = Long.class) })
	List<ProcPart> listActSelectedParts(@Param("procDefId") Long procDefId, @Param("actDefId") Long actDefId);

	@Select("select t2.part_id, t2.part_name, t2.part_type, t3.filter_sql from proc_act_part t1 left join proc_part t2 on t1.part_id = t2.part_id left join proc_act_filter t3 on t3.part_id = t1.part_id and t3.act_def_id = t1.act_def_id where t1.act_def_id = #{actDefId} and t2.proc_def_id = #{procDefId}")
	@Results({ @Result(property = "partId", column = "part_id", javaType = Long.class),
		@Result(property = "partName", column = "part_name", javaType = String.class),
		@Result(property = "partType", column = "part_type", javaType = String.class),
		@Result(property = "partFilterSql", column = "filter_sql", javaType = Long.class) })
	List<WfActPart> listWfActPartByActDefId(@Param("procDefId")Long procDefId, @Param("actDefId") Long actDefId);
}